package com.sap.sap_mangers.service.Impl;

import com.sap.sap_mangers.dto.sapImage.SapImageDto;
import com.sap.sap_mangers.mapper.ImageMapper;
import com.sap.sap_mangers.pojo.SapImage;
import com.sap.sap_mangers.service.CacheService;
import com.sap.sap_mangers.service.ImageService;
import com.sap.sap_mangers.until.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author 樊东升
 * @date 2022/8/23 17:36
 */
@Service
public class ImageServiceImpl implements ImageService {
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private CosUntil cosUntil;

    @Override
    public SimpleJson addImage(List<MultipartFile> images) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        CosUntil cosUntil = new CosUntil();
        SapImage sapImage = new SapImage(JwtUtil.getUserForToken(), cacheService.getRealNameCache(cacheService.getRealNameCacheMap(), JwtUtil.getUserForToken()), new Timestamp(System.currentTimeMillis()), 0);
        for (MultipartFile image : images) {
            if (FileCheckUntil.checkImage(image)) {
                try {
                    sapImage.setImageUrl(cosUntil.getCosUrl(image, "SapImageWall/" + image.getOriginalFilename()));
                } catch (IOException e) {
                    return new SimpleJson(1, "图片上传出错");
                }
                try {
                    sapImage.setImageId(idWorker.nextId());
                    imageMapper.addImage(sapImage);
                } catch (Exception e) {
                    e.printStackTrace();
                    return new SimpleJson(1, "数据库出错");
                }
            }
        }
        return SimpleJson.SUCCESS;
    }

    @Override
    public SimpleJson getImageAudit() {
        List<SapImage> imageAudit = imageMapper.getImageAudit();
        for (SapImage sapImage : imageAudit) {
            sapImage.setImageIdForStr(String.valueOf(sapImage.getImageId()));
        }
        return new SimpleJson(imageAudit);
    }

    @Override
    public void passImage(SapImageDto sapImageDto) {
        SapImage sapImage = new SapImage(JwtUtil.getUserForToken(), cacheService.getRealNameCache(cacheService.getRealNameCacheMap(), JwtUtil.getUserForToken()), new Timestamp(System.currentTimeMillis()));
        for (Long aLong : sapImageDto.getImageIdForLong()) {
            sapImage.setImageId(aLong);
            imageMapper.passImage(sapImage);
        }
    }

    @Override
    public void noPassImage(SapImageDto sapImageDto) {
        SapImage sapImage = new SapImage(JwtUtil.getUserForToken(), cacheService.getRealNameCache(cacheService.getRealNameCacheMap(), JwtUtil.getUserForToken()), new Timestamp(System.currentTimeMillis()));
        for (Long aLong : sapImageDto.getImageIdForLong()) {
            SapImage imageById = imageMapper.getImageById(aLong);
            String imageUrl = imageById.getImageUrl();
            cosUntil.deleteCosUrl(imageUrl);
            sapImage.setImageId(aLong);
            imageMapper.noPassImage(sapImage);
        }

    }

    @Override
    public List<SapImage> getImage() {
        List<SapImage> image = imageMapper.getImage();
        for (SapImage sapImage : image) {
            sapImage.setImageIdForStr(String.valueOf(sapImage.getImageId()));
        }
        return image;
    }

    @Override
    public SimpleJson deleteImage(Long id) {
        SapImage sapImage = new SapImage(JwtUtil.getUserForToken(), cacheService.getRealNameCache(cacheService.getRealNameCacheMap(), JwtUtil.getUserForToken()), new Timestamp(System.currentTimeMillis()));
        SapImage imageById = imageMapper.getImageById(id);
        String imageUrl = imageById.getImageUrl();
        cosUntil.deleteCosUrl(imageUrl);
        sapImage.setImageId(id);
        imageMapper.deleteImage(sapImage);
        return SimpleJson.SUCCESS;
    }
}
